package com.zbbt.sportslottery.db;

import java.util.ArrayList;
import java.util.List;

import android.R.integer;
import android.R.string;
import android.content.Context;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.bean.BallDataBean;
import com.zbbt.sportslottery.bean.ColumnBean;
import com.zbbt.sportslottery.bean.DrawNotice;
import com.zbbt.sportslottery.bean.FindingImg;
import com.zbbt.sportslottery.bean.FindingNewsInformation;
import com.zbbt.sportslottery.bean.FocusData;
import com.zbbt.sportslottery.bean.News;
import com.zbbt.sportslottery.bean.NewsCategory;
import com.zbbt.sportslottery.bean.NewsImg;
import com.zbbt.sportslottery.bean.NewsProgarm;
import com.zbbt.sportslottery.bean.NewsStatus;

/**
 * 
 * @ClassName: DbManager
 * @Description: 数据库管理
 * @author：LiZhimin
 * @date：2014-12-8 下午5:35:31
 * @version V1.0
 */
public class DbManager {

	private static final String DB_NAME = "DB_FJTC";
	private static Context context;
	private static DbUtils db;

	static {
		context = App.getAppContext();
		db = DbUtils.create(context, DB_NAME);
		db.configAllowTransaction(true);
		db.configDebug(true);
		try {
			db.createTableIfNotExist(NewsCategory.class);
			db.createTableIfNotExist(NewsStatus.class);
			db.createTableIfNotExist(DrawNotice.class);
			db.createTableIfNotExist(ColumnBean.class);
		} catch (DbException e) {
			e.printStackTrace();
		}
	}

	private DbManager() {}

	/**
	 * 
	 * @MethodName:saveNewsList
	 * @Description: 缓存新闻列表
	 * @author：LiZhimin
	 * @date：2014-12-12 下午5:06:47
	 * @param list
	 * @param category
	 *            新闻条目分类 void
	 */
	public static void saveNewsList(List<News> list, String programaId) {
		try {
			if (programaId.equals("1")) {
				// 每次存储之前先把原来的删除
				db.delete(News.class, WhereBuilder.b("ishot", "=", programaId));
			} else {
				// 每次存储之前先把原来的删除
				db.delete(News.class, WhereBuilder.b("programaId", "=", programaId).and("ishot", "<", "1"));
			}
			ArrayList<NewsImg> newsimglist = new ArrayList<NewsImg>();
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).getNewsImg() != null)
					newsimglist.addAll(list.get(i).getNewsImg());
			}
			ArrayList<Long> sqllist = new ArrayList<Long>();
			for (int i = 0; i < newsimglist.size(); i++) {
				sqllist.add(newsimglist.get(i).getId());
			}
			db.delete(NewsImg.class, WhereBuilder.b("id", "in", sqllist));
			db.saveAll(list);
			db.saveAll(newsimglist);
		} catch (DbException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @MethodName:deleteNewsList
	 * @Description: 删除新闻列表
	 * @author：LiZhimin
	 * @date：2014-12-12 下午5:06:47
	 * @param list
	 * @param category
	 *            新闻条目分类 void
	 */
	public static void deleteNewsList(String programaId) {
		try {
			if (programaId == null || programaId.isEmpty()) {
				db.deleteAll(News.class);
			} else {
				db.delete(News.class, WhereBuilder.b("isShow", "=", programaId));
			}
		} catch (DbException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @MethodName:saveFindList
	 * @Description: 缓存发现列表
	 * @author：wwh
	 * @date：2015-03-11 下午5:06:47
	 * @param list
	 * @param category
	 * 
	 */
	public static void saveFindList(List<FindingNewsInformation> list) {
		try {
			db.deleteAll(FindingNewsInformation.class);
			db.deleteAll(FindingImg.class);
			ArrayList<FindingImg> findimglist = new ArrayList<FindingImg>();
			if (list != null) {
				for (int i = 0; i < list.size(); i++) {
					if (list.get(i).getRelImg() != null)
						findimglist.addAll(list.get(i).getRelImg());
				}
			}
			db.saveAll(list);
			db.saveAll(findimglist);
		} catch (DbException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @MethodName:getNewsList
	 * @Description: 获取新闻列表
	 * @author：LiZhimin
	 * @date：2014-12-12 下午5:06:59
	 * @param category
	 * @param pageSize
	 * @param pageIndex
	 * @return List<InformationItemData>
	 */
	public static List<News> getNewsList(String programaId, int pageSize, int pageIndex) {
		try {
			List<News> list = new ArrayList<News>();
			if (programaId.equals("1")) {
				list = db.findAll(Selector.from(News.class).where("ishot", "=", programaId).limit(pageSize).offset(pageSize * pageIndex));
			} else {
				list = db.findAll(Selector.from(News.class).where("programaId", "=", programaId).and("ishot", "<", "1").limit(pageSize).offset(pageSize * pageIndex));
			}
			if (list != null) {
				for (int i = 0; i < list.size(); i++) {
					list.get(i).addImg(db.findAll(Selector.from(NewsImg.class).where("newsId", "=", list.get(i).getNewsId())));
				}
			}
			return list;
		} catch (DbException e) {
			e.printStackTrace();
		}
		return null;

	};

	/**
	 * 
	 * @MethodName:getFindList
	 * @Description: 获取发现列表
	 * @author：wwh
	 * @date：2014-03-11 下午5:06:59
	 * @param category
	 * @param pageSize
	 * @param pageIndex
	 * @return List<FindingNewsInformation>
	 */
	public static List<FindingNewsInformation> getFindList(int pageSize, int pageIndex) {
		try {
			List<FindingNewsInformation> list = new ArrayList<FindingNewsInformation>();
			list = db.findAll(Selector.from(FindingNewsInformation.class).limit(pageSize).offset(pageSize * pageIndex));
			if (list != null) {
				for (int i = 0; i < list.size(); i++) {
					list.get(i).addImg(db.findAll(Selector.from(FindingImg.class).where("relId", "=", list.get(i).getFindId())));
				}
			}
			return list;
		} catch (DbException e) {
			e.printStackTrace();
		}
		return null;

	};

	/**
	 * 
	 * @MethodName:savefoucelist
	 * @Description: 缓存焦点图片
	 * @author：kl
	 * @date：2014-12-12 下午5:06:47
	 * @param list
	 */
	public static void savefoucelist(List<FocusData> list) {
		try {
			// 每次存储之前先把原来的删除
			db.deleteAll(FocusData.class);
			db.saveAll(list);
		} catch (DbException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @MethodName:getFouceList
	 * @Description: 获取焦点图片列表
	 * @author：kl
	 * @date：2014-12-12 下午5:06:59
	 */
	public static List<FocusData> getFouceList() {
		try {
			return db.findAll(FocusData.class);
		} catch (DbException e) {
			e.printStackTrace();
		}
		return null;
	};

	/**
	 * 
	 * @MethodName:saveNewsRead
	 * @Description: 保存已读新闻id
	 * @author：LiZhimin
	 * @date：2014-12-12 下午5:22:13
	 * @param data
	 *            void
	 */
	public static void saveNewsRead(String newsId) {
		NewsStatus newsStatus;
		try {
			newsStatus = db.findFirst(Selector.from(NewsStatus.class).where("newsId", "=", newsId));
			if (newsStatus == null) {
				newsStatus = new NewsStatus(newsId);
				db.saveOrUpdate(newsStatus);
			}
		} catch (DbException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @MethodName:getReadNewsList
	 * @Description: 获取已读新闻列表
	 * @author：LiZhimin
	 * @date：2014-12-12 下午5:25:56
	 * @return List<NewsStatus>
	 */
	public static NewsStatus getReadNews(String idvalue) {
		try {
			return db.findFirst((Selector.from(NewsStatus.class).where("newsId", "=", idvalue)));
		} catch (DbException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 保存栏目列表
	 */
	public static void saveProgarmList(List<NewsProgarm> list) {
		try {
			db.deleteAll(NewsProgarm.class);
			db.saveOrUpdateAll(list);
		} catch (DbException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取栏目列表
	 */
	public static List<NewsProgarm> getProgarmList() {
		try {
			return db.findAll(NewsProgarm.class);
		} catch (DbException e) {
			e.printStackTrace();
		}
		return null;
	}

	// /**
	// *
	// * @MethodName:categoryCount
	// * @Description: 保存新闻分类点击次数
	// * @author：LiZhimin
	// * @date：2014-12-8 下午5:20:40
	// * @param index
	// * 条目角标 void
	// */
	// public static void categoryCount(int index) {
	// try {
	// int NewsCategorynum = Constant.CATEGORY_IDS.length;
	// List<NewsCategory> categories = db.findAll(NewsCategory.class);
	// if (categories != null) {
	// if (categories.size() > 0) {
	// NewsCategory category =
	// db.findFirst((Selector.from(NewsCategory.class).where("name", "=",
	// Constant.CATEGORY_IDS[index])));
	// category.setCount(category.getCount() + 1);
	// db.saveOrUpdate(category);
	// } else {
	// for (int i = 1; i < NewsCategorynum; i++) {
	// NewsCategory category = new NewsCategory(i + "",
	// Constant.CATEGORY_IDS[i], 1);
	// db.saveOrUpdate(category);
	// }
	// }
	// }
	// } catch (DbException e) {
	// e.printStackTrace();
	// }
	// }

	// /**
	// *
	// * @MethodName:categoryCount
	// * @Description: 返回具体栏目点击数量
	// * @author：kl
	// * @date：2014-12-12 下午5:25:56
	// * @return List<NewsStatus>
	// */
	// public static List<NewsCategory> getCategoryCount() {
	// try {
	// int NewsCategorynum = Constant.CATEGORY_IDS.length;
	// List<NewsCategory> categories = db.findAll(NewsCategory.class);
	// if (categories != null) {
	// if (categories.size() > 0) {
	// if (NewsCategorynum > categories.size() + 1) {
	// List<NewsCategory> countlist =
	// db.findAll(Selector.from(NewsCategory.class).orderBy("count"));
	// int num = countlist.get(categories.size() - 1).getCount();
	// NewsCategory category = new NewsCategory(categories.size() + 1 + "",
	// Constant.CATEGORY_IDS[NewsCategorynum - 1], num + 1);
	// db.saveOrUpdate(category);
	// } else if (NewsCategorynum < categories.size() + 1) {
	// db.deleteAll(NewsCategory.class);
	// for (int i = 1; i < NewsCategorynum; i++) {
	// NewsCategory category = new NewsCategory(i + "",
	// Constant.CATEGORY_IDS[i], 1);
	// db.saveOrUpdate(category);
	// }
	// }
	// }
	// }
	// return db.findAll(Selector.from(NewsCategory.class).orderBy("count"));
	// } catch (DbException e) {
	// e.printStackTrace();
	// }
	// return null;
	// }

	/**
	 * 
	 * @MethodName: saveAwardlist
	 * @Description: 保存开奖数据
	 * @author: donn
	 * @date: 2014年12月16日 上午11:50:04
	 * @param list
	 *            void
	 */
	public static void saveAwardlist(List<BallDataBean> list) {
		try {
			// 每次存储之前先把原来的删除
			db.deleteAll(BallDataBean.class);
			db.saveOrUpdateAll(list);
		} catch (DbException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @MethodName:savaChannel
	 * @Description: 储存频道的位置信息
	 * @author：GaomingShuo
	 * @date：2015-2-5 下午5:48:10
	 * @param columnBean
	 *            void
	 */
	public static void savaChannel(List<ColumnBean> columnList) {
		try {
			db.deleteAll(ColumnBean.class);
			db.saveAll(columnList);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static List<ColumnBean> getChannel() {
		try {
			return db.findAll(ColumnBean.class);
		} catch (DbException e) {
			e.printStackTrace();
		}
		return null;

	}

	/**
	 * 
	 * @MethodName: getAwardList
	 * @Description: 获取开奖数据
	 * @author: donn
	 * @date: 2014年12月16日 上午11:55:16
	 * @return List<AwardnoticeData>
	 */
	public static List<BallDataBean> getAwardList() {
		try {
			return db.findAll(BallDataBean.class);
		} catch (DbException e) {
			e.printStackTrace();
		}
		return null;
	};

}
